package com.sniff.atp.controller;

import com.alibaba.fastjson2.JSONObject;
import com.sniff.atp.constant.LoginConstant;
import com.sniff.atp.dto.ContentDTO;
import com.sniff.atp.entity.ToolsInfo;
import com.sniff.atp.manager.ToolsInfoMananager;
import com.sniff.atp.response.SniffResponse;
import com.sniff.atp.service.wms.storage.WMSCombinationOperationService;
import com.sniff.atp.service.wms.storage.WMSStorageService;
import com.sniff.atp.utils.DBUtil;
import com.sniff.atp.utils.JsonUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @BelongsProject: sniff-atp
 * @BelongsPackage: com.sniff.atp.controller
 * @Author: zxy
 * @CreateTime: 2022-11-14 19:52
 * @Description: 仓库入库流程
 * @Version: 1.0
 */
@Api(value = "入库模块", tags = "入库模块")
@RestController
@RequestMapping("/storage")
public class StorageInputController {
    @Autowired
    private WMSStorageService wmsStorageService;
    @Autowired
    private WMSCombinationOperationService wmsCombinationOperationService;
    @Autowired
    private ToolsInfoMananager toolsInfoMananager;

    @ApiOperation("订单号：一键入库")
    @GetMapping("/orderInput")
    @ApiImplicitParams ({@ApiImplicitParam(name = "platformOrderNo", value = "订单号", required = true)})
    public Object orderInput(String platformOrderNo){
        ToolsInfo toolsInfo = toolsInfoMananager.selectOne(19L);
        toolsInfo.setCount(toolsInfo.getCount() + 1);
        toolsInfoMananager.updateOne(toolsInfo);
        wmsStorageService.storageProcess(LoginConstant.wmsLoginDTO, platformOrderNo);
        return SniffResponse.pass("");
    }
//
//    @ApiOperation("采购单号：一键入库")
//    @GetMapping("/purchaseInput")
//    @ApiImplicitParams ({@ApiImplicitParam(name = "purchaseNo", value = "采购单号", required = true),
//            @ApiImplicitParam(name = "warehouse", value = "仓库code", required = true)})
//    public Object purchaseInput(String purchaseNo, String warehouse){
//        ToolsInfo toolsInfo = toolsInfoMananager.selectOne(20L);
//        toolsInfo.setCount(toolsInfo.getCount() + 1);
//        toolsInfoMananager.updateOne(toolsInfo);
//        wmsStorageService.storageProcess(LoginConstant.wmsLoginDTO, purchaseNo, warehouse);
//        return SniffResponse.pass("");
//    }

    @ApiOperation("采购单号：一键入库(新)")
    @GetMapping("/purchaseInputNew")
    @ApiImplicitParams ({@ApiImplicitParam(name = "purchaseNo", value = "采购单号", required = true)})
    public Object purchaseInput(String purchaseNo){
        ToolsInfo toolsInfo = toolsInfoMananager.selectOne(21L);
        toolsInfo.setCount(toolsInfo.getCount() + 1);
        toolsInfoMananager.updateOne(toolsInfo);
        wmsStorageService.storageProcessV1(LoginConstant.wmsLoginDTO, purchaseNo);
        return SniffResponse.pass("");
    }

    @ApiOperation("批次包：一键成品上架")
    @GetMapping("/pkgCodeInput")
    @ApiImplicitParams ({@ApiImplicitParam(name = "pkgCode", value = "批次号", required = true)})
    public Object pkgCodeInput(String pkgCode){
        ToolsInfo toolsInfo = toolsInfoMananager.selectOne(29L);
        toolsInfo.setCount(toolsInfo.getCount() + 1);
        toolsInfoMananager.updateOne(toolsInfo);
        wmsStorageService.doTransferAndShelve(LoginConstant.wmsLoginDTO, pkgCode);
        return SniffResponse.pass("");
    }

    @ApiOperation("根据订单组合任务：一键组合完成，入成品区")
    @GetMapping("/combinationOrderInput")
    @ApiImplicitParams ({@ApiImplicitParam(name = "platformOrderNo", value = "订单编号", required = true)})
    public Object combinationOrderInput(String platformOrderNo){
        ToolsInfo toolsInfo = toolsInfoMananager.selectOne(22L);
        toolsInfo.setCount(toolsInfo.getCount() + 1);
        toolsInfoMananager.updateOne(toolsInfo);

        wmsCombinationOperationService.platformOrderCombinationProcess(LoginConstant.wmsLoginDTO, platformOrderNo);
        return SniffResponse.pass("");
    }

    @ApiOperation("根据组合任务：一键组合完成，入成品区")
    @GetMapping("/combinationPackInput")
    @ApiImplicitParams ({@ApiImplicitParam(name = "packCode", value = "组合任务编号", required = true)})
    public Object combinationPackInput(String packCode){
        ToolsInfo toolsInfo = toolsInfoMananager.selectOne(23L);
        toolsInfo.setCount(toolsInfo.getCount() + 1);
        toolsInfoMananager.updateOne(toolsInfo);

        //根据组合编号查询仓库
        List<JSONObject> SQL016 = DBUtil.select("WMS-根据组合编号查询仓库", new ContentDTO("", packCode));
        String wareHouse = SQL016.get(0).getString("warehouse");

        //组合完成并上架
        String s = wmsCombinationOperationService.combinationProcess(LoginConstant.wmsLoginDTO, packCode, wareHouse);

        String pkgCode = JsonUtil.strToJSONObject(s).getString("pkgCode");
        // 组合主商品移库至成品区
        wmsCombinationOperationService.combinationStorage(LoginConstant.wmsLoginDTO, pkgCode);
//        wmsStorageService.doTransferAndShelve(LoginConstant.wmsLoginDTO, pkgCode);
        return SniffResponse.pass("");
    }
}
